<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="490px" :title="`${drawerProps.title}员工`">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-card header="员工基本信息" shadow="never">
        <el-form-item label="员工角色" prop="roleIds">
          <el-select v-model="drawerProps.row!.roleIds" placeholder="请选择员工角色" filterable multiple collapse-tags>
            <el-option v-for="item in roleList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="员工姓名" prop="name">
          <el-input v-model="drawerProps.row!.name" placeholder="请填写员工姓名" clearable></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="drawerProps.row!.phone" placeholder="请填写手机号" clearable></el-input>
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="drawerProps.row!.idCard" placeholder="请填写身份证号" clearable></el-input>
        </el-form-item>
        <el-form-item label="身高" prop="height">
          <el-input v-model="drawerProps.row!.height" placeholder="请填写身高单位（CM）" clearable style="width: 120px">
          </el-input>
          <span style="margin-left: 10px">CM</span>
        </el-form-item>
        <el-form-item label="体重" prop="height">
          <el-input v-model="drawerProps.row!.weight" placeholder="请填写身高单位（CM）" clearable style="width: 120px">
          </el-input>
          <span style="margin-left: 10px">KG</span>
        </el-form-item>
        <el-form-item label="工作经验" prop="height">
          <el-input v-model="drawerProps.row!.workYear" placeholder="几年工作经验" clearable style="width: 120px"> </el-input>
          <span style="margin-left: 10px">年</span>
        </el-form-item>
        <el-form-item label="户籍所在地" prop="registeredResidence">
          <el-input v-model="drawerProps.row!.registeredResidence" placeholder="请填写身份证号" clearable></el-input>
        </el-form-item>
        <el-form-item label="学历" prop="educational">
          <el-select v-model="drawerProps.row!.educational" placeholder="请选择学历" clearable>
            <el-option v-for="item in educational" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="居住地址" prop="address">
          <el-input v-model="drawerProps.row!.address" placeholder="请填写居住地址" clearable></el-input>
        </el-form-item>
      </el-card>
      <el-tabs type="border-card" class="employee-tabs" style="margin-top: 15px">
        <el-tab-pane label="头像">
          <UploadImg
            v-model:image-url="drawerProps.row!.avatarUrl"
            width="420px"
            height="420px"
            :file-size="10"
            :file-tag="'员工-头像'"
          >
            <template #empty>
              <el-icon><Avatar /></el-icon>
              <span>请上传头像</span>
            </template>
          </UploadImg>
        </el-tab-pane>
        <el-tab-pane label="健康证">
          <UploadImg
            v-model:image-url="drawerProps.row!.healthCertificateUrl"
            width="420px"
            height="420px"
            :file-size="10"
            :file-tag="'员工-健康证'"
          >
            <template #empty>
              <el-icon><Avatar /></el-icon>
              <span>请上传健康证 </span>
            </template>
          </UploadImg>
        </el-tab-pane>
        <el-tab-pane label="护理证">
          <UploadImg
            v-model:image-url="drawerProps.row!.nursingCertificateUrl"
            width="420px"
            height="420px"
            :file-size="10"
            :file-tag="'员工-护理证'"
          >
            <template #empty>
              <el-icon><Avatar /></el-icon>
              <span>请上传护理证 </span>
            </template>
          </UploadImg>
        </el-tab-pane>
        <el-tab-pane label="荣誉证书">
          <UploadImg
            v-model:image-url="drawerProps.row!.honorCertificateUrl"
            width="420px"
            height="420px"
            :file-size="10"
            :file-tag="'员工-荣誉证书'"
          >
            <template #empty>
              <el-icon><Avatar /></el-icon>
              <span>请上传荣誉证书 </span>
            </template>
          </UploadImg>
        </el-tab-pane>
        <el-tab-pane label="工作照片">
          <UploadImgs
            v-model:file-list="drawerProps.row!.photoUrls"
            height="140px"
            width="201px"
            :file-tag="'员工-工作照片'"
            :file-size="10"
          >
            <template #empty>
              <el-icon><Picture /></el-icon>
              <span>请上传照片</span>
            </template>
          </UploadImgs>
        </el-tab-pane>
      </el-tabs>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="EmployeeDrawer">
import { ref, reactive } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { Employee } from "@/api/interface";
import UploadImg from "@/components/Upload/Img.vue";
import UploadImgs from "@/components/Upload/Imgs.vue";
import { getRoleList } from "@/api/modules/role";
import { Role } from "@/api/interface/index";
import { educational } from "@/utils/dict";
import { ElNotification } from "element-plus";

const roleList = ref<Role.ResRoleList[]>([]);

const rules = reactive({
  roleIds: [{ required: true, message: "请选择员工角色" }],
  name: [{ required: true, message: "请填写员工姓名" }],
  phone: [{ required: true, message: "请填写手机号" }],
  gender: [{ required: true, message: "请选择性别" }],
  idCard: [{ required: true, message: "请填写身份证号" }]
});

interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<Employee.ResEmployeeList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {}
});

// 接收父组件传过来的参数
const acceptParams = async (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;

  if (roleList.value.length == 0) {
    // 加载角色列表
    const reqRoleParams: Role.ReqRoleParams = {};
    const res = await getRoleList(reqRoleParams);
    roleList.value = res.data || [];
  }
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      const res = await drawerProps.value.api!(drawerProps.value.row);
      if (drawerProps.value.title == "新增") {
        ElNotification({
          title: `新增【${drawerProps.value.row.name}】员工成功`,
          message: `初始密码: ${res.data}，已发送员工手机号`,
          type: "success"
        });
      }
      ElMessage.success({ message: `${drawerProps.value.title}用户成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>

<style lang="css" scoped>
.employee-tabs .el-tab-pane {
  padding-top: 5px;
}
</style>
